<?php
global $_W, $_GPC;
$settings = $this->module['config'];
if ($settings['switch'] == 0) {
	message('积分商城已关闭！', '', 'error');
}
$request_array = $_GPC;
$uid = $request_array['uid'];
foreach ($request_array as $key => $val) {
	$unsetkeyarr = array('i', 'do', 'm', 'c');
	if (in_array($key, $unsetkeyarr) || strstr($key, '__')) {
		unset($request_array[$key]);
	}
}
$ret = $this->parseCreditConsume($settings['appkey'], $settings['appsecret'], $request_array);
if (is_array($ret)) {
	$insert = array('uniacid' => $_W['uniacid'], 'uid' => $uid, 'bizId' => date('YmdHi') . random(8, 1), 'orderNum' => $request_array["orderNum"], 'credits' => $request_array["credits"], 'params' => $request_array["params"], 'type' => $request_array["type"], 'ip' => $request_array["ip"], 'starttimestamp' => $request_array["timestamp"], 'waitAudit' => $request_array["waitAudit"], 'actualPrice' => $request_array["actualPrice"], 'description' => $request_array["description"], 'facePrice' => $request_array["facePrice"], 'Audituser' => $request_array["Audituser"], 'itemCode' => $request_array["itemCode"], 'status' => 0, 'createtime' => time());
	pdo_insert($this->tableduiba, $insert);
	if (pdo_insertid()) {
		load()->model('mc');
		$usercredits = mc_credit_fetch($uid, $types = array('credit1'));
		$yue = intval($usercredits['credit1']) - $request_array["credits"];
		if ($yue > 0) {
			$updatecredit = mc_credit_update($uid, 'credit1', -abs($request_array["credits"]), array("兑吧", "兑吧兑换--" . $request_array["description"], 'tyzm_duiba'));
			if ($updatecredit) {
				die(json_encode(array('status' => 'ok', 'errorMessage' => "", 'bizId' => $insert['bizId'], 'credits' => $yue)));
			} else {
				die(json_encode(array('status' => 'fail', 'errorMessage' => '扣除积分错误', 'credits' => $request_array["credits"])));
			}
		} else {
			die(json_encode(array('status' => 'fail', 'errorMessage' => '积分不足', 'credits' => $request_array["credits"])));
		}
	} else {
		die(json_encode(array('status' => 'fail', 'errorMessage' => '系统错误，请重试！', 'credits' => $request_array["credits"])));
	}
} else {
	die(json_encode(array('status' => 'fail', 'errorMessage' => $ret, 'credits' => $request_array["credits"])));
}